﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TMDT_Nhom1A.DTO;

namespace TMDT_Nhom1A.utils
{
    public class CouponUtil
    {
        public static int GetMaxRowFromOrderCouponTable(QL_DoanhNghiepDataContext qldoanhnghiep)
        {
            
            //dem so dong trong CSDL
            int maxRow = 0;
            maxRow = (from orderCoupon in qldoanhnghiep.ORDER_COUPONs
                          select orderCoupon).Count();

            return maxRow;

            
        }

        public static HttpCookie WriteCookie(HttpCookie cookie, string cookieKey, string value)
        {
            cookie[cookieKey] = value;
            return cookie;
        }

        //lay webservice van chuyen dang xai
        public static string GetUrlTransportWebservice(QL_DoanhNghiepDataContext qldoanhnghiep, string transCompanyId)
        {
            try
            {
                //tim trong bang transport webservice xem cot Active nao = true thi lay
                var result = from trans in qldoanhnghiep.TRANSPORT_WEBSERVICEs
                             where trans.TRANSPORT_WS_ID == int.Parse(transCompanyId)
                             select trans;

                if (result.Count() == 1)
                {
                    TRANSPORT_WEBSERVICE transWebService = result.Single();
                    return transWebService.TransportUrl;
                }
                return String.Empty;
            }
            catch
            {
                return String.Empty;
            }
        }

        //lay webservice van chuyen dang xai
        public static int GetIDTransportWebservice(QL_DoanhNghiepDataContext qldoanhnghiep)
        {
            try
            {
                //tim trong bang transport webservice xem cot Active nao = true thi lay
                var result = from trans in qldoanhnghiep.TRANSPORT_WEBSERVICEs
                             where trans.Active == true
                             select trans;

                if (result.Count() == 1)
                {
                    TRANSPORT_WEBSERVICE transWebService = result.Single();
                    return transWebService.TRANSPORT_WS_ID;
                }
                return -1;
            }
            catch
            {
                return -1;
            }
        }

        //lay webservice mua chung ung voi userID đat validationCode do
        public static string GetUrlMuaChungWebservice(QL_DoanhNghiepDataContext qldoanhnghiep,int userID)
        {
            try
            {
                //tim trong bang register webservice xem cot Active nao = true thi lay
                var result = from reg in qldoanhnghiep.REGISTRY_WEBSERVICEs
                             where reg.UserId == userID
                             select new 
                             {
                                 reg.MuaChungWS_Url
                             };

                if (result.Count() == 1)
                {
                    foreach (var item in result)
                    {
                        return item.MuaChungWS_Url;
                    }
                }

                return String.Empty;
            }
            catch
            {
                return String.Empty;
            }
        }

    }
}
